<?php

namespace app\admin\controller\yalimedia;

use app\admin\model\User;
use app\admin\model\yalimedia\LectureAllocation;
use app\admin\model\yalimedia\LectureUser;
use app\common\controller\Backend;
use app\common\model\Lecture as LectureModel;

/**
 * 课程
 *
 * @icon fa fa-circle-o
 */
class Lecture extends Backend
{

    protected $noNeedRight = [];
    /**
     * Order模型对象
     * @var \app\admin\model\yalimedia\Lecture
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new LectureModel;

    }

    public function add()
    {
        if ($this->request->isAjax()) {

            $aid = session('admin.id');
            $row = $this->request->post()['row'];
            $row = array_merge($row,['createtime'=>time(), 'updatetime'=>time()]);

            if($aid == 1){
                $row['status'] = 1;
            }
            $this->model->insert($row);
            $this->success('成功');
        }
        return $this->view->fetch();
    }

    /**
     * 查看
     */
    public function index()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();

            $list = $this->model
                ->where($where)
                ->order($sort, $order)
                ->paginate($limit);

             foreach ($list->items() as &$v){

                 $v['type'] = $v['type']==1?'现场':'直播';
             }
            $result = array("total" => $list->total(), "rows" => $list->items());

            return json($result);
        }
        return $this->view->fetch();
    }

    public function allocation()
    {
        if ($this->request->isAjax()) {

            $gid = input()['group_id'];
            $uid = input()['user_id'];
            LectureAllocation::where('lecture_id',input('ids'))->delete();
            LectureUser::where('lecture_id',input('ids'))->delete();

            $group = array_map(function($v){return $v ? ['group_id'=>$v,'lecture_id'=>input('ids'),'createtime'=>time(),'updatetime'=>time()] : [];},$gid);
            LectureAllocation::insertAll($group);

            $user = array_map(function($v){return $v ? ['user_id'=>$v,'lecture_id'=>input('ids'),'createtime'=>time(),'updatetime'=>time()] : [];},$uid);
            LectureUser::insertAll($user);




            $this->success('成功');
        }

        $group = \app\admin\model\UserGroup::column('id,name');
        $user = User::where('status','normal')->column('id,username as name');
        $this->assign('id',input('ids'));
        $ids = LectureAllocation::where('lecture_id',input('ids'))->column('group_id');
        $uids = LectureUser::where('lecture_id',input('ids'))->column('user_id');
        $this->view->assign('groupList', build_select('group_id[]', $group, $ids, ['class' => 'form-control selectpicker', 'multiple'=>'']));
        $this->view->assign('userList', build_select('user_id[]', $user, $uids, ['class' => 'form-control selectpicker', 'multiple'=>'']));
        return $this->view->fetch();
    }

    public function check($ids)
    {
        if ($this->request->isAjax()) {
            $this->model->where('id',$ids)->update(['status'=>1]);
            $this->success("审核成功", null, ['id' => $ids]);
        }

    }


}
